{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>天然气可供量(亿立方米)</th>\n",
       "      <th>天然气生产量(亿立方米)</th>\n",
       "      <th>进口天然气量(亿立方米)</th>\n",
       "      <th>出口天然气量(-)(亿立方米)</th>\n",
       "      <th>年初年末天然气库存差额(亿立方米)</th>\n",
       "      <th>天然气能源消费总量(亿立方米)</th>\n",
       "      <th>农、林、牧、渔业天然气消费总量(亿立方米)</th>\n",
       "      <th>工业天然气消费总量(亿立方米)</th>\n",
       "      <th>建筑业天然气消费总量(亿立方米)</th>\n",
       "      <th>交通运输、仓储和邮政业天然气消费总量(亿立方米)</th>\n",
       "      <th>批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)</th>\n",
       "      <th>其他天然气消费总量(亿立方米)</th>\n",
       "      <th>居民生活天然气消费总量(亿立方米)</th>\n",
       "      <th>天然气平衡差额(亿立方米)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-01-01</td>\n",
       "      <td>3773.8</td>\n",
       "      <td>2155.5</td>\n",
       "      <td>1673.5</td>\n",
       "      <td>55.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3773.0</td>\n",
       "      <td>1.7</td>\n",
       "      <td>2678.2</td>\n",
       "      <td>3.2</td>\n",
       "      <td>366.3</td>\n",
       "      <td>70.2</td>\n",
       "      <td>61.0</td>\n",
       "      <td>592.3</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>3340.2</td>\n",
       "      <td>1994.9</td>\n",
       "      <td>1397.0</td>\n",
       "      <td>51.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3339.9</td>\n",
       "      <td>1.3</td>\n",
       "      <td>2304.0</td>\n",
       "      <td>2.6</td>\n",
       "      <td>354.3</td>\n",
       "      <td>62.1</td>\n",
       "      <td>55.6</td>\n",
       "      <td>560.0</td>\n",
       "      <td>0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>3057.5</td>\n",
       "      <td>1761.7</td>\n",
       "      <td>1331.8</td>\n",
       "      <td>36.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3059.7</td>\n",
       "      <td>1.2</td>\n",
       "      <td>2092.1</td>\n",
       "      <td>2.8</td>\n",
       "      <td>341.5</td>\n",
       "      <td>62.5</td>\n",
       "      <td>57.3</td>\n",
       "      <td>502.3</td>\n",
       "      <td>-2.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-01</td>\n",
       "      <td>2814.3</td>\n",
       "      <td>1601.6</td>\n",
       "      <td>1246.4</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2817.1</td>\n",
       "      <td>1.3</td>\n",
       "      <td>1940.1</td>\n",
       "      <td>2.5</td>\n",
       "      <td>286.2</td>\n",
       "      <td>60.8</td>\n",
       "      <td>57.9</td>\n",
       "      <td>468.4</td>\n",
       "      <td>-2.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-01-01</td>\n",
       "      <td>2390.7</td>\n",
       "      <td>1480.4</td>\n",
       "      <td>945.6</td>\n",
       "      <td>35.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2393.7</td>\n",
       "      <td>1.1</td>\n",
       "      <td>1575.2</td>\n",
       "      <td>1.8</td>\n",
       "      <td>284.7</td>\n",
       "      <td>57.6</td>\n",
       "      <td>52.9</td>\n",
       "      <td>420.3</td>\n",
       "      <td>-3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date  天然气可供量(亿立方米)  天然气生产量(亿立方米)  进口天然气量(亿立方米)  出口天然气量(-)(亿立方米)  \\\n",
       "0  2021-01-01        3773.8        2155.5        1673.5             55.2   \n",
       "1  2020-01-01        3340.2        1994.9        1397.0             51.7   \n",
       "2  2019-01-01        3057.5        1761.7        1331.8             36.1   \n",
       "3  2018-01-01        2814.3        1601.6        1246.4             33.6   \n",
       "4  2017-01-01        2390.7        1480.4         945.6             35.3   \n",
       "\n",
       "   年初年末天然气库存差额(亿立方米)  天然气能源消费总量(亿立方米)  农、林、牧、渔业天然气消费总量(亿立方米)  工业天然气消费总量(亿立方米)  \\\n",
       "0                0.0           3773.0                    1.7           2678.2   \n",
       "1                0.0           3339.9                    1.3           2304.0   \n",
       "2                0.0           3059.7                    1.2           2092.1   \n",
       "3                0.0           2817.1                    1.3           1940.1   \n",
       "4                0.0           2393.7                    1.1           1575.2   \n",
       "\n",
       "   建筑业天然气消费总量(亿立方米)  交通运输、仓储和邮政业天然气消费总量(亿立方米)  批发和零售业、住宿和餐饮业天然气消费总量(亿立方米)  \\\n",
       "0               3.2                     366.3                        70.2   \n",
       "1               2.6                     354.3                        62.1   \n",
       "2               2.8                     341.5                        62.5   \n",
       "3               2.5                     286.2                        60.8   \n",
       "4               1.8                     284.7                        57.6   \n",
       "\n",
       "   其他天然气消费总量(亿立方米)  居民生活天然气消费总量(亿立方米)  天然气平衡差额(亿立方米)  \n",
       "0             61.0              592.3            0.8  \n",
       "1             55.6              560.0            0.3  \n",
       "2             57.3              502.3           -2.2  \n",
       "3             57.9              468.4           -2.8  \n",
       "4             52.9              420.3           -3.0  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "# 读取CSV文件\n",
    "df = pd.read_csv('../ClearData/Natural_gas_20_bfill_date.csv')\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 假设你想预测的列名是'target'\n",
    "target = df['天然气生产量(亿立方米)'].values\n",
    "\n",
    "# 将数据归一化到0-1之间\n",
    "scaler = MinMaxScaler(feature_range=(0, 1))\n",
    "target_scaled = scaler.fit_transform(target.reshape(-1, 1))\n",
    "\n",
    "# 创建训练数据\n",
    "X_train = []\n",
    "look_back = 3 # 假设你想使用过去10天的数据来预测未来\n",
    "\n",
    "for i in range(look_back, len(target_scaled)):\n",
    "    X_train.append(target_scaled[i-look_back:i, 0])\n",
    "\n",
    "# 将列表转换为numpy数组\n",
    "X_train = np.array(X_train)\n",
    "\n",
    "# 重塑为LSTM所需的形状 [samples, time steps, features]\n",
    "X_train = np.reshape(X_train, (X_train.shape[0],1, X_train.shape[1]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
